
// 导入路由
import VueRouter from 'vue-router'

// 注册路由
import Vue from 'vue'
Vue.use(VueRouter)

// 创建路由实例
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/HomeView'),
      meta: { title: '主页' }
    },
    {
      path: '/news',
      name: 'news',
      component: () => import('@/views/NewsView'),
      meta: { title: '分类' }
    },
    {
      path: '/sport',
      name: 'sport',
      redirect: { name: 'sport-home' },
      component: () => import('@/views/SportView'),
      meta: { title: '体育' },
      // 子路由
      children: [{
        path: 'home',
        name: 'sport-home',
        component: () => import('@/views/sport/SportHome'),
        meta: { title: '体育 - 首页' }
      },
      {
        path: 'china',
        name: 'sport-china',
        component: () => import('@/views/sport/SportChina'),
        meta: { title: '体育 - 国内' }
      },
      {
        path: ':other',
        name: 'sport-foreign',
        component: () => import('@/views/sport/SportForeign'),
        meta: { title: '体育 - 国外' }
      }]
    },
    {
      path: '*',
      name: 'err404',
      component: () => import('@/views/NotFound'),
      meta: { title: '页面未找到' }
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) document.title = to.meta.title // 页面标题
  next()
})

export default router
